/**
 * 表情包数据库
 * 
 * 定义表情包数据结构和按情感分类的表情包集合
 * Requirements: 3.1, 3.2
 */

import type { Emoji, EmojiDatabase, EmotionalTone } from '../types';

/**
 * 所有情感类型列表
 */
export const ALL_EMOTIONAL_TONES: EmotionalTone[] = [
  'happy',
  'sad',
  'comfort',
  'funny',
  'grateful',
  'apologetic',
  'neutral',
];

/**
 * 表情包数据
 * 使用 Unicode emoji 字符，每个情感类型至少包含3个表情包，满足 Requirements 3.1
 */
const emojis: Emoji[] = [
  // ============================================
  // Happy - 开心类表情包
  // ============================================
  {
    id: 'happy-001',
    url: '😊',
    category: 'happy',
    tags: ['开心', '微笑', '高兴', '愉快'],
    description: '开心微笑',
  },
  {
    id: 'happy-002',
    url: '😄',
    category: 'happy',
    tags: ['大笑', '哈哈', '开心', '搞笑'],
    description: '开心大笑',
  },
  {
    id: 'happy-003',
    url: '🎉',
    category: 'happy',
    tags: ['庆祝', '撒花', '开心', '成功'],
    description: '庆祝撒花',
  },
  {
    id: 'happy-004',
    url: '👍',
    category: 'happy',
    tags: ['点赞', '棒', '好', '赞同'],
    description: '竖起大拇指',
  },
  {
    id: 'happy-005',
    url: '🥳',
    category: 'happy',
    tags: ['派对', '庆祝', '开心', '狂欢'],
    description: '派对狂欢',
  },

  // ============================================
  // Sad - 悲伤类表情包
  // ============================================
  {
    id: 'sad-001',
    url: '😢',
    category: 'sad',
    tags: ['哭泣', '难过', '伤心', '流泪'],
    description: '伤心哭泣',
  },
  {
    id: 'sad-002',
    url: '😞',
    category: 'sad',
    tags: ['失望', '沮丧', '难过', '无奈'],
    description: '失望沮丧',
  },
  {
    id: 'sad-003',
    url: '💔',
    category: 'sad',
    tags: ['心碎', '伤心', '难过', '失恋'],
    description: '心碎了',
  },
  {
    id: 'sad-004',
    url: '😭',
    category: 'sad',
    tags: ['大哭', '伤心', '难过', '崩溃'],
    description: '大哭',
  },

  // ============================================
  // Comfort - 安慰类表情包
  // ============================================
  {
    id: 'comfort-001',
    url: '🤗',
    category: 'comfort',
    tags: ['拥抱', '安慰', '温暖', '关心'],
    description: '温暖拥抱',
  },
  {
    id: 'comfort-002',
    url: '🫂',
    category: 'comfort',
    tags: ['摸头', '安慰', '鼓励', '加油'],
    description: '互相拥抱',
  },
  {
    id: 'comfort-003',
    url: '❤️',
    category: 'comfort',
    tags: ['爱心', '关心', '温暖', '支持'],
    description: '送你爱心',
  },
  {
    id: 'comfort-004',
    url: '💪',
    category: 'comfort',
    tags: ['加油', '鼓励', '支持', '打气'],
    description: '为你加油',
  },
  {
    id: 'comfort-005',
    url: '🌈',
    category: 'comfort',
    tags: ['彩虹', '希望', '美好', '未来'],
    description: '雨后彩虹',
  },

  // ============================================
  // Funny - 调侃/幽默类表情包
  // ============================================
  {
    id: 'funny-001',
    url: '🐶',
    category: 'funny',
    tags: ['狗头', '调侃', '搞笑', '滑稽'],
    description: '狗头保命',
  },
  {
    id: 'funny-002',
    url: '🤭',
    category: 'funny',
    tags: ['捂脸', '无语', '搞笑', '尴尬'],
    description: '捂脸笑',
  },
  {
    id: 'funny-003',
    url: '😏',
    category: 'funny',
    tags: ['坏笑', '调侃', '得意', '狡黠'],
    description: '坏笑',
  },
  {
    id: 'funny-004',
    url: '😱',
    category: 'funny',
    tags: ['震惊', '惊讶', '搞笑', '夸张'],
    description: '震惊表情',
  },
  {
    id: 'funny-005',
    url: '🤣',
    category: 'funny',
    tags: ['笑哭', '搞笑', '太好笑', '绝了'],
    description: '笑哭了',
  },

  // ============================================
  // Grateful - 感激类表情包
  // ============================================
  {
    id: 'grateful-001',
    url: '🙇',
    category: 'grateful',
    tags: ['鞠躬', '感谢', '谢谢', '感激'],
    description: '鞠躬感谢',
  },
  {
    id: 'grateful-002',
    url: '🙏',
    category: 'grateful',
    tags: ['祈祷', '感谢', '拜托', '感激'],
    description: '双手合十',
  },
  {
    id: 'grateful-003',
    url: '🌸',
    category: 'grateful',
    tags: ['送花', '感谢', '美好', '祝福'],
    description: '送你花花',
  },
  {
    id: 'grateful-004',
    url: '🥰',
    category: 'grateful',
    tags: ['感动', '感谢', '开心', '幸福'],
    description: '感动开心',
  },

  // ============================================
  // Apologetic - 道歉类表情包
  // ============================================
  {
    id: 'apologetic-001',
    url: '😔',
    category: 'apologetic',
    tags: ['抱歉', '对不起', '道歉', '歉意'],
    description: '抱歉表情',
  },
  {
    id: 'apologetic-002',
    url: '🙇‍♂️',
    category: 'apologetic',
    tags: ['鞠躬', '道歉', '对不起', '请原谅'],
    description: '鞠躬道歉',
  },
  {
    id: 'apologetic-003',
    url: '😅',
    category: 'apologetic',
    tags: ['流汗', '尴尬', '抱歉', '不好意思'],
    description: '尴尬流汗',
  },
  {
    id: 'apologetic-004',
    url: '🥺',
    category: 'apologetic',
    tags: ['可怜', '求原谅', '抱歉', '委屈'],
    description: '求原谅',
  },

  // ============================================
  // Neutral - 中性类表情包
  // ============================================
  {
    id: 'neutral-001',
    url: '👌',
    category: 'neutral',
    tags: ['好的', 'OK', '可以', '同意'],
    description: 'OK手势',
  },
  {
    id: 'neutral-002',
    url: '🤔',
    category: 'neutral',
    tags: ['思考', '想想', '考虑', '嗯'],
    description: '思考中',
  },
  {
    id: 'neutral-003',
    url: '👋',
    category: 'neutral',
    tags: ['挥手', '你好', '再见', '打招呼'],
    description: '挥手打招呼',
  },
  {
    id: 'neutral-004',
    url: '☕',
    category: 'neutral',
    tags: ['咖啡', '休息', '喝茶', '放松'],
    description: '喝杯咖啡',
  },
  {
    id: 'neutral-005',
    url: '😐',
    category: 'neutral',
    tags: ['无语', '平静', '冷漠', '淡定'],
    description: '面无表情',
  },
];

/**
 * 构建按情感分类的表情包ID映射
 */
function buildCategoryMap(emojiList: Emoji[]): Record<EmotionalTone, string[]> {
  const categories: Record<EmotionalTone, string[]> = {
    happy: [],
    sad: [],
    comfort: [],
    funny: [],
    grateful: [],
    apologetic: [],
    neutral: [],
  };

  for (const emoji of emojiList) {
    categories[emoji.category].push(emoji.id);
  }

  return categories;
}

/**
 * 表情包数据库实例
 */
export const emojiDatabase: EmojiDatabase = {
  emojis,
  categories: buildCategoryMap(emojis),
};

/**
 * 根据ID获取表情包
 */
export function getEmojiById(id: string): Emoji | undefined {
  return emojiDatabase.emojis.find((emoji) => emoji.id === id);
}

/**
 * 根据情感类型获取表情包列表
 */
export function getEmojisByCategory(category: EmotionalTone): Emoji[] {
  const ids = emojiDatabase.categories[category];
  return ids.map((id) => getEmojiById(id)).filter((e): e is Emoji => e !== undefined);
}

/**
 * 获取所有表情包
 */
export function getAllEmojis(): Emoji[] {
  return [...emojiDatabase.emojis];
}

/**
 * 搜索表情包（按标签或描述）
 */
export function searchEmojis(query: string): Emoji[] {
  const lowerQuery = query.toLowerCase();
  return emojiDatabase.emojis.filter(
    (emoji) =>
      emoji.tags.some((tag) => tag.toLowerCase().includes(lowerQuery)) ||
      emoji.description.toLowerCase().includes(lowerQuery)
  );
}
